<template>
	<view class="container">
		<view class="myTopBox">
			<template v-if="login_user">
				<view class="myTopAvatar">
					<image class="myTopAvatar-image" :src="userInfo.avatar" mode="scaleToFill">
					</image>
				</view>
				<view class="myTopNick">{{userInfo.nickname}}</view>
			</template>
			<template v-else>
				<view class="mylogin">
					<u-button @click="getInlogin" style="width: 160rpx;" type="warning" :plain="true" text="登录">
					</u-button>
				</view>
			</template>
		</view>

		<view class="mylist">
			<u-cell-group>
				<u-cell title="个人信息" :rightIconStyle="{fontSize:'32rpx'}" isLink @click="getInPersonalInfo"></u-cell>
				<u-cell title="房屋信息管理" :rightIconStyle="{fontSize:'32rpx'}" isLink @click="getInHousingInfo"></u-cell>
					<u-cell title="我的活动" :rightIconStyle="{fontSize:'32rpx'}" isLink @click="getMyActivity"></u-cell>
				<u-cell title="访客登记" :rightIconStyle="{fontSize:'32rpx'}" isLink @click="getVisitor"></u-cell>
				<u-cell title="用户行程" :rightIconStyle="{fontSize:'32rpx'}" isLink @click="getTripInfo"></u-cell>
				<u-cell title="投诉意见及建议" :rightIconStyle="{fontSize:'32rpx'}" isLink @click="getFeedback"></u-cell>
			</u-cell-group>

		</view>
		<view class="mylist">
			<u-cell-group>
				<u-cell :titleStyle="{'color': 'red',}" :rightIconStyle="{fontSize:'32rpx'}" title="退出当前帐号" 
				@click="getQuitAccount " isLink>
				</u-cell>
			</u-cell-group>
		</view>
		<u-modal :show="deleteShow" :title="deleteTitle" showCancelButton buttonReverse :zoom="false"
			:closeOnClickOverlay="true" @confirm="deleteConfirm" @cancel="deleteShow = false"
			@close="deleteShow = false "></u-modal>
	</view>
</template>

<script>
	import api from "@/api/api.js"
	import {
		mapActions,
		mapState,
		mapMutations
	} from "vuex"
	export default {
		name: 'my',
		components: {
		
			
		},
		data() {
			return {
				login_user: false,
				userInfo: null,
				deleteShow:false,
				deleteTitle:''
			}
		},
		created() {
			this.getIsLoginUserInfo()
			

		},
		onShow(){
			this.userInfo = uni.getStorageSync("userInfo");
		},
		mounted() {},
		destroyed() {},
		computed: {
			...mapState(['islogin'])
		},
		methods: {
			getQuitAccount(){
				this.deleteShow = true;
				this.deleteTitle = `是否确认退出当前账号`
			},
			deleteConfirm(){
				uni.removeStorageSync("userInfo")
				uni.removeStorageSync("wxUserInfo")
				this.userInfo = null
				this.login_user = false
				this.deleteShow = false;
			},
			// 前往个人信息
			getInPersonalInfo() {
				const that = this;
				if (!that.login_user) {
					uni.$u.toast('请先登录')
					return
				}
				uni.$u.route({
					type: "navigateTo",
					url: "pages/personalInfo/index",
					params: {
						login_user: this.login_user ? 1 : 0,
					}
				})
			},
			// 前往房屋信息管理
			getInHousingInfo() {
				const that = this;
				if (!that.login_user) {
					uni.$u.toast('请先登录')
					return
				}
				if (that.userInfo.type !== 0) {
					uni.$u.toast('非业主，无法查看')
					return
				}
				uni.navigateTo({
					url: "/pages/housingInfo/list"
				})
			},
			// 前往行程信息管理
			getTripInfo() {
				const that = this;
			
				if (!that.login_user) {
					uni.$u.toast('请先登录')
					return
				}
				uni.$u.route({
					type: "navigateTo",
					url: "/pages/trip/index",

				})
			},
			// 我的活动
			getMyActivity(){
				const that = this;
				
				if (!that.login_user) {
					uni.$u.toast('请先登录')
					return
				}
			
				uni.$u.route({
					type: "navigateTo",
					url: '/pages/activity/index',
					params: {
						current:1
					}
				})
			},
			// 前往访客登记
			getVisitor() {
				const that = this;
				if (!that.login_user) {
					uni.$u.toast('请先登录')
					return
				}
				uni.$u.route({
					type: "navigateTo",
					url: '/pages/visitor/list',

				})

			},
			// 前往意见反馈
			getFeedback() {
				const that = this;
				if (!that.login_user) {
					uni.$u.toast('请先登录')
					return
				}
				uni.$u.route({
					type: "navigateTo",
					url: '/pages/feedback/list',

				})
			},
			// 登录
			getInlogin() {
				// 获取微信授权信息，opneid,判断是否是新用户,

				//新用户：跳转至填写表单页面。
				// 带参数
				api.getOauth2Url().then(res => {
					let url = res.data.data;
					console.log('getOauth2Url', res.data.data)
					window.location.replace(url)
				}).catch(err => {
					console.log(err)
				})
			},
			// 获取地址上code的值
			getUrlParam(name) {
				return new Promise((resolve, reject) => {
					var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
					var r = window.location.search.substr(1).match(reg);
					if (r != null) {
						resolve(decodeURI(r[2]))
					} else {
						resolve(false)
					}
				})

			},
			// 获取微信用户信息
			async getIsLoginUserInfo() {
				const that = this;
				let userInfo = uni.getStorageSync("userInfo");
				let wxUserInfo = uni.getStorageSync('wxUserInfo')
				console.log('wxUserInfo', !!wxUserInfo.openId)
				if (!!wxUserInfo.openId) {
					this.getUserInfoByOpenid()
					return
				}
				if (!!userInfo) {
					that.userInfo = userInfo;
					that.login_user = true
					return;
				}
				that.login_user = false
				let code = await this.getUrlParam('code');
				console.log('code', code)
				if (!code) return;
				let data = {
					code
				}
				await api.getCodeLogin(data).then(res => {
					console.log('getCodeLogin', res.data.data)
					uni.setStorageSync('wxUserInfo', res.data.data)
					if (res.data.code == 10001) {
						// 已注册
						this.getUserInfoByOpenid()
					} else if (res.data.code == 10000) {
						// 未注册
						uni.$u.route({
							type: "navigateTo",
							url: "pages/personalInfo/index",
							params: {
								myCode: res.data.code
							}
						})
					}

				}).catch(err => {
					console.log('codeerr', err)
				})
			},
			// 个人信息
			getUserInfoByOpenid() {
				const that = this;
				let wxUserInfo = uni.getStorageSync('wxUserInfo')

				// if(!wxUserInfo.openId) return
				let data = {
					weixinOpenid: wxUserInfo.openId
				}
				api.getUserInfoByOpenid(data).then(res => {
					console.log('getUserInfoByOpenid', res.data.rows)
					if (res.data.code == 200) {
						that.userInfo = res.data.rows[0];
						uni.setStorageSync('userInfo', that.userInfo)
						that.login_user = true
					}else if(res.data.code == 601 ){
						uni.$u.route({
							type: "navigateTo",
							url: "pages/personalInfo/index",
							params: {
								login_user: this.login_user ? 1 : 0
							}
						})
					}
				}).catch(err => {
					console.log(err)
				})

			},
		},
	}
</script>

<style lang="scss" scoped>
	.container {
		width: 100%;
		overflow: hidden;
	}

	.myTopBox {
		width: 100%;
		height: 280rpx;
		padding-bottom: 32rpx;
		padding-top: 32rpx;
		background-color: #FFFFFF;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;

		.myTopAvatar {
			width: 160rpx;
			height: 160rpx;
			border-radius: 50%;
			border: 1px solid rgba($color: #FFFFFF, $alpha: 0.6);
			background-color: #FFFFFF;
			display: flex;
			align-items: center;
			justify-content: center;

			.myTopAvatar-image {
				width: calc(100% - 2px);
				height: calc(100% - 2px);
				border-radius: 50%;
				display: block;
			}
		}

		.myTopNick {
			height: 60rpx;
			line-height: 60rpx;
			font-size: 26rpx;
			margin: 16rpx 0;
		}
	}

	.mylist {
		width: 100%;
		margin: 16rpx 0;
		background-color: #FFFFFF;
	}
</style>
<style>
	.mylist .u-icon__icon {
		font-size: 32rpx !important;

	}

	.mylogin {}
</style>
